<template>
	<div class="system-cart-container standard-container layout-padding layout-pd">
		<div class="review-list-title">
			<div class="title-actions">
				<!-- <el-input v-model="state.search.name" size="default" clearable placeholder="请输入商品名称" style="max-width: 180px"></el-input> -->
				<el-button size="default" type="primary" class="ml10" @click="getTableData">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
			</div>
		</div>
		<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
			<el-table-column prop="title" label="商品名称" show-overflow-tooltip></el-table-column>
			<el-table-column prop="image" label="商品图片" show-overflow-tooltip>
				<template #default="scope">
					<div style="width: 80px; height: 80px">
						<el-image
							v-if="scope.row.image != ''"
							:src="STATIC_URL + scope.row.image"
							fit="cover"
							class="image"
							:preview-src-list="[STATIC_URL + scope.row.image]"
							:preview-teleported="true"
						/>
					</div>
				</template>
			</el-table-column>

			<el-table-column label="商品属性" show-overflow-tooltip>
				<template #default="scope">
					<div class="goods-sku-text">
						<span v-for="(sku, index) in scope.row.skus" :key="sku" :class="{ ml5: index != 0 }"> {{ sku }}, </span>
					</div>
				</template>
			</el-table-column>

			<el-table-column prop="quantity" label="数量" show-overflow-tooltip></el-table-column>
			<el-table-column prop="price" label="价格" show-overflow-tooltip></el-table-column>
			<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
			<el-table-column prop="nickname" label="用户昵称" show-overflow-tooltip></el-table-column>
			<el-table-column label="操作" width="100">
				<template #default="scope">
					<!--						<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onOpen(scope.row.id)"-->
					<!--							>修改-->
					<!--						</el-button>-->
					<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onRowDel(scope.row)">删除 </el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="pagination-container">
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				:page-sizes="[10, 15, 20, 30, 50, 100]"
				v-model:current-page="state.tableData.paging.currentPage"
				background
				v-model:page-size="state.tableData.paging.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.paging.totalCount"
			>
			</el-pagination>
		</div>
		<CartDialog ref="CartDialogRef"></CartDialog>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { STATIC_URL } from '/@/utils/config';
import { CartApi } from '/@/api/shop/cart';
import msg from '/@/utils/messageUtils';
import { ElMessage, ElMessageBox } from 'element-plus';

const CartDialog = defineAsyncComponent(() => import('./CartDialog.vue'));
const CartDialogRef = ref();

const state = reactive({
	isToggle: false,
	search: {
		name: '',
	},
	tableData: {
		data: [],
		total: 0,
		loading: false,
		paging: {
			currentPage: 1,
			pageSize: 15,
			totalCount: 0,
			totalPages: 0,
		},
	},
});

const onOpen = (id: any) => {
	console.log('id: ', id);
	CartDialogRef.value.open(id);
};

const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除购物车商品名称：“${row.title}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await CartApi().delete(row.id);
			getTableData();
			ElMessage.success('删除成功');
		})
		.catch(() => {});
};

const refresh = () => {
	getTableData();
};
const getTableData = async () => {
	try {
		const { data } = await CartApi().search({
			offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
			limit: state.tableData.paging.pageSize,
		});
		state.tableData.data = data.datas;
		state.tableData.paging.currentPage = data.paging.currentPage;
		state.tableData.paging.totalCount = data.paging.totalCount;
		console.log('result: ', data);
	} catch (e) {
		msg().error('查询失败，请稍后重试');
	}
};
// 分页改变
const onHandleSizeChange = (pageSize: number) => {
	state.tableData.paging.pageSize = pageSize;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
	state.tableData.paging.currentPage = currentPage;
	getTableData();
};

onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-cart-container {
	.el-table {
		flex: 1;
	}
}
</style>
